﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net.Http;

namespace SiteApp.BLL.Common
{
    public class ImageHelper
    {
        /// <summary>
        /// 获取网络路劲的图片
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static Image GetHttpImage(string url)
        {

            var client = new HttpClient();

            var uri = new Uri(Uri.EscapeUriString(url));
            byte[] urlContents = client.GetByteArrayAsync(uri).Result;

            using (var ms = new System.IO.MemoryStream(urlContents))
            {
                return Image.FromStream(ms);
            }
        }


        /// <summary>
        /// 将Base64字符串转换为Image对象
        /// </summary>
        /// <param name="base64Str">base64字符串</param>
        /// <returns></returns>
        public static Bitmap Base64StrToImage(string base64Str)
        {
            Bitmap bitmap = null;

            try
            {
                byte[] arr = Convert.FromBase64String(base64Str);
                MemoryStream ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);
                ms.Close();
                bitmap = bmp;
            }
            catch (Exception ex)
            {
            }

            return bitmap;
        }

        /// <summary>
        /// 图片加字
        /// </summary>
        /// <param name="old"></param>
        /// <param name="text"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static Bitmap ImgDrawString(Bitmap old,string text,int x,int y)
        {
                #region  图片加字

                Bitmap bmp = new Bitmap(old);

                Graphics g = Graphics.FromImage(bmp);
              
                Font font = new Font("宋体", 15);
                SolidBrush sbrush = new SolidBrush(Color.Black);
                g.DrawString(text, font, sbrush, new PointF(x, y));
                MemoryStream ms = new MemoryStream();


                #endregion

            return bmp;
        }

        /// <summary>
        /// 图片加矩形
        /// </summary>
        /// <param name="old"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Bitmap ImgDrawRectangle(Bitmap old,int x,int y,int width,int height)
        {
                Bitmap bmp = new Bitmap(old);

                #region 矩形

                Graphics g = Graphics.FromImage(bmp);

                Brush brush = new SolidBrush(Color.Black);
                Pen pen = new Pen(brush, 1);
                pen.DashStyle = DashStyle.DashDot;

                g.DrawRectangle(pen, new Rectangle(x, y, width, height));

                g.Dispose();

                #endregion

            return bmp;
        }

        /// <summary>
        /// 图片加圆
        /// </summary>
        /// <param name="old"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Bitmap ImgDrawEllipse(Bitmap old, int x, int y, int width, int height)
        {
            Graphics g = Graphics.FromImage(old);

            Brush brush = new SolidBrush(Color.Black);
            Pen pen = new Pen(brush, 1);
            pen.DashStyle = DashStyle.DashDot;

            g.DrawEllipse(pen, new Rectangle(x, y, width, height));

            g.Dispose();

            return old;

        }

        /// <summary>
        /// 图片加线
        /// </summary>
        /// <param name="old"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <returns></returns>
        public static Bitmap ImaDrawLine(Bitmap old, int x, int y, int x1, int y1)
        {

            Graphics g = Graphics.FromImage(old);

            Brush brush = new SolidBrush(Color.Black);

            Pen pen = new Pen(brush, 1);
        

            pen.DashStyle = DashStyle.DashDot;

            g.DrawLine(pen, x, y, x1, y1);

            g.Dispose();

            return old;
        }

        /// <summary>
        /// 图片根据XY修改RGB
        /// </summary>
        /// <param name="old"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="r"></param>
        /// <param name="g"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Bitmap ImaDrawPixel(Bitmap old, int x, int y, int r, int g, int b)
        {
            var rgb = old.GetPixel(x, y);

            old.SetPixel(x, y, Color.FromArgb(r, g, b));

            return old;
        }


    }
}
